<?php
require_once '../includes/class.phpmailer.php';

$emailid=$_REQUEST['sendcustommessage'];

$msgEditScat="";
if(isset($_POST['Submit']))
 {


	$subject=$_REQUEST['subject'];		
	$body=$_REQUEST['content'];
	$today=date("Y-m-d");
	//if all type of user
	if($_POST['selType']=="All")
	{
	$sqlAll="select * from tbl_users";
	$resAll=mysql_query($sqlAll);
	while($rowAll=mysql_fetch_array($resAll))
	{
	$sql="select * from tbl_users where email='".$rowAll['email']."'";
	$res=mysql_query($sql);
	$rowi=mysql_fetch_array($res);		
mysql_query("insert into tbl_messages(senderid,recieverid,subject,message,posteddate,status,type) values('0','".$rowi['user_id']."','".$subject."','".addslashes($body)."','".date("Y-m-d")."','1','From Admin')");
		
						
			}
		}
	else if($_POST['selUser']=="All")
	{
	$sqlAll="select * from tbl_users where 	user_type='".$_POST['utype']."'";
	$resAll=mysql_query($sqlAll);
	while($rowAll=mysql_fetch_array($resAll))
	{
				$sql="select * from tbl_users where email='".$rowAll['email']."'";
	$res=mysql_query($sql);
	$rowi=mysql_fetch_array($res);		
mysql_query("insert into tbl_messages(senderid,recieverid,subject,message,posteddate,status,type) values('0','".$rowi['user_id']."','".$subject."','".addslashes($body)."','".date("Y-m-d")."','1','From Admin')");
		

			
			}
		}
	else
	{
	$sql="select * from tbl_users where email='".$_POST['selUser']."'";
	$res=mysql_query($sql);
	$rowi=mysql_fetch_array($res);		
mysql_query("insert into tbl_messages(senderid,recieverid,subject,message,posteddate,status,type) values('0','".$rowi['user_id']."','".$subject."','".addslashes($body)."','".date("Y-m-d")."','1','From Admin')");
		
	
		}
		$msgEditScat="Message has been sent";
 }
 $email_subject="";
 $email_body="";
 if($emailid)
 {
$sqlEmail="select * from tbl_customemailmessage where id='".$emailid."'";
$rstEmail=mysql_query($sqlEmail);
$rowEmail=mysql_fetch_array($rstEmail);
$email_subject=$rowEmail['title'];
$email_body=$rowEmail['content'];
 }
?>
<script type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
	tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect",
		
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example content CSS (should be your site CSS)
		content_css : "css/content.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "js/template_list.js",
		external_link_list_url : "js/link_list.js",
		external_image_list_url : "js/image_list.js",
		media_external_list_url : "js/media_list.js",

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
</script>
<table width="100%"  border="1" cellspacing="0" class="container">
  <tr>
    <td width="469" height="25" class="tdHeader">Send Custom Message</td>
  </tr>
  <tr>
    <td  height="577">
	
          <form action="" method="post" name="formEditSubCat" id="formEditSubCat">
		  
           <table width="100%" border="0" cellspacing="0" align="center">
          <tr>
            <td height="40" colspan="4"><div align="center"><?php if($msgEditScat!="") {echo "<b>".$msgEditScat."</b>"; }?></div></td>
          </tr>
		 
		
          <tr>
            <td width="1%" height="29">&nbsp;</td>
            <td width="23%">Select User Type</td>
            <td width="14%"><label>
              <select name="selType" id="selType" onChange="javascript:getUsers();">
			   <option value="">Select Type</option>
			  <option value="All">All</option>
			  <option value="3">Merchant</option>
			  <option value="2">Seller</option>
			  <option value="5">Wholeseller</option>
			  <option value="1">Buyer</option>
              </select>
            </label></td>
            <td width="62%"><div id="loading"></div></td>
          </tr>
          <tr>
            <td height="29">&nbsp;</td>
            <td><div align="left">Custom Subject</div></td>
            <td colspan="2">
              <div align="left">
                <input type="text" name="subject" id="subject" value="<?php echo $email_subject; ?>">
              </div></td>
          </tr>
		   <tr>
            <td height="31">&nbsp;</td>
            <td>Custom Content</td>
            <td colspan="2"><textarea name="content" cols="55" rows="15" id="content"><?php echo $email_body; ?></textarea>
             <input type="hidden" name="utype" id="utype"/></td>
           </tr>		  
		  <tr>
            <td height="65" colspan="4"><div align="center">
                <input name="Submit" type="submit" value="Submit" class="button">
              </div></td>
         </tr>
        </table>
       </form>
     </td>
  </tr>
</table>

